/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package locusts.common.vibro;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;

/**
 *
 * @author Hamish Morgan
 */
public class VTMessage implements Serializable {

    private int rows;
    private int cols;
    private transient ByteBuffer buf;

    private VTMessage(int rows, int cols, ByteBuffer buf) {
        this.rows = rows;
        this.cols = cols;
        this.buf = buf;
    }

    private void writeObject(ObjectOutputStream out) throws IOException {
        out.writeInt(rows);
        out.writeInt(cols);
        out.write(buf.array());
    }

    private void readObject(ObjectInputStream in) throws IOException {
        rows = in.readInt();
        cols = in.readInt();
        byte[] data = new byte[rows * cols];
        in.read(data);
        buf = ByteBuffer.wrap(data);
    }

    public int getRows() {
        return rows;
    }

    public int getCols() {
        return cols;
    }

    public ByteBuffer getBytes() {
        return buf;
    }

    public static VTMessage createFrom(VTAccumulator vta) {
        return new VTMessage(vta.getRows(), vta.getCols(), vta.getBuffer());
    }

    public VTMessage transpose() {
        byte[] in = buf.array();
        byte[] out = new byte[buf.array().length];
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                out[i * cols + j] = in[j * rows + i];
        return new VTMessage(cols, rows, ByteBuffer.wrap(out));
    }

    public VTMessage minValue(int val) {
        byte[] in = buf.array();
        byte[] out = new byte[buf.array().length];
        for (int i = 0; i < in.length; i++) {
            if (in[i] == 0)
                out[i] = 0;
            else if (in[i] < val)
                out[i] = (byte) (val & 0xff);
            else
                out[i] = in[i];
//            out[i] = (in[i] & 0xff) < val ? (byte)val : in[i];
        }
        return new VTMessage(rows, cols, ByteBuffer.wrap(out));

    }

    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer();
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (j != 0) sb.append(' ');
                sb.append(0xff & buf.get(i * cols + j));
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (getClass() != obj.getClass()) return false;
        final VTMessage other = (VTMessage) obj;
        if (this.rows != other.rows) return false;
        if (this.cols != other.cols) return false;
        if (this.buf != other.buf &&
                (this.buf == null || !this.buf.equals(other.buf)))
            return false;
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 23 * hash + this.rows;
        hash = 23 * hash + this.cols;
        hash = 23 * hash + (this.buf != null ? this.buf.hashCode() : 0);
        return hash;
    }
}
